package com.crm.util;

import java.security.SecureRandom;

/**
 * 随机字符串生成工具类
 */
public class RandomStringUtil {
    
    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static final int DEFAULT_LENGTH = 8;
    private static final SecureRandom random = new SecureRandom();
    
    /**
     * 生成默认长度(8位)的随机字符串，包含字母和数字
     * 
     * @return 8位随机字符串
     */
    public static String generateRandomString() {
        return generateRandomString(DEFAULT_LENGTH);
    }
    
    /**
     * 生成指定长度的随机字符串，包含字母和数字
     * 
     * @param length 字符串长度
     * @return 指定长度的随机字符串
     */
    public static String generateRandomString(int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(randomIndex));
        }
        return sb.toString();
    }
}
